package com.ae.content.web;

import com.ae.content.dto.JsonResponse;
import com.ae.content.dto.ResourceDTO;
import com.ae.content.dto.ResourceStatus;
import com.ae.content.dto.upload.*;
import com.ae.content.dto.upload.response.ResponseData;
import com.ae.content.service.AliexpressService;
import com.alibaba.fastjson.JSON;
import com.global.iop.api.IopClient;
import com.global.iop.api.IopClientImpl;
import com.global.iop.api.IopRequest;
import com.global.iop.api.IopResponse;
import com.global.iop.domain.Protocol;
import com.global.iop.util.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/aliexpress")
@ResponseBody
@CrossOrigin(origins = "*")
public class AliexpressController {

    @Autowired
    private AliexpressService aliexpressService;

    @PostMapping("/upload")
    public ResponseData upload(@RequestBody GeneratedJsonClass generatedJsonClass) throws ApiException {
        IopClient client = new IopClientImpl("https://api-sg.aliexpress.com", "504368", "9YXgCPaIN7iu5AISehgpNRoHiiwRmGXw");
        IopRequest request = new IopRequest();
        request.setApiName("aliexpress.influencer.content.video.publish");
        request.addApiParameter("request_param", JSON.toJSONString(generatedJsonClass));
        System.out.println("-------------" + JSON.toJSONString(generatedJsonClass));
        IopResponse response = client.execute(request, Protocol.TOP);
        String gopResponseBody = response.getGopResponseBody();
        System.out.println("-------------" + gopResponseBody);
        ResponseData responseData = JSON.parseObject(gopResponseBody, ResponseData.class);
        return responseData;
    }

    @PostMapping("/resource")
    public JsonResponse resource(@RequestBody ResourceDTO resourceDTO) throws ApiException {
        IopClient client = new IopClientImpl("https://api-sg.aliexpress.com", "504368", "9YXgCPaIN7iu5AISehgpNRoHiiwRmGXw");
        IopRequest request = new IopRequest();
        request.setApiName("aliexpress.influencer.content.resource.upload");
        request.addApiParameter("request_param", JSON.toJSONString(resourceDTO));
        System.out.println("-------------" + JSON.toJSONString(resourceDTO));
        IopResponse response = client.execute(request, Protocol.TOP);
        JsonResponse jsonResponse = JSON.parseObject(response.getGopResponseBody(), JsonResponse.class);
        System.out.println("-------------" + JSON.toJSONString(jsonResponse));
        List<ResourceStatus> resourceStatuses = jsonResponse.getResult().getData().getResourceStatuses();
        if(!CollectionUtils.isEmpty(resourceStatuses)) {
            for(ResourceStatus resourceStatus : resourceStatuses) {
                resourceStatus.setResourceIdStr(resourceStatus.getResourceId() + "");
            }
        }
        return jsonResponse;
    }

    public static void main(String[] args) {
        GeneratedJsonClass generatedJsonClass = new GeneratedJsonClass();
        generatedJsonClass.setContent("内容");
        generatedJsonClass.setCurrency("国家");
        generatedJsonClass.setEmail("qianshali710@gmail.com");
        generatedJsonClass.setLocale("地区");
        generatedJsonClass.setVideo_resource_id("89526524444568802");
        generatedJsonClass.setCover_img_resource_id("1111111111111");
        Attributes attributes = new Attributes();
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("key", "value");
        attributes.setAttributes(objectObjectHashMap);
        generatedJsonClass.setAttributes(attributes);
        List<Item> items = new ArrayList<>();
        Item item = new Item();
        item.setItemId("111111111");
        items.add(item);
        generatedJsonClass.setItems(items);
        List<ExternalItem> externalItems = new ArrayList<>();
        ExternalItem externalItem = new ExternalItem();
        externalItem.setTitle("标题");
        externalItem.setSource("来源");
        externalItem.setItem_url("www.baidu.com");
        externalItem.setItem_price("价格");
        externalItem.setItem_main_pic("www.meitu.com");
        List<String[]> itemPics = new ArrayList<>();
        String[] picArray = {"xxxx", "xxxx"};
        String[] picArray2 = {"aaa", "bbb", "ccc"};
        itemPics.add(picArray);
        itemPics.add(picArray2);
        externalItem.setItem_pics(itemPics);
        externalItems.add(externalItem);
        generatedJsonClass.setExternal_items(externalItems);
        List<Hashtag> hashtags = new ArrayList<>();
        Hashtag hashtag = new Hashtag();
        hashtag.setStart("101");
        hashtag.setEnd("110");
        hashtag.setContent("内容");
        hashtag.setObject_type("0");
        hashtag.setObject_id("10086");
        hashtags.add(hashtag);
        generatedJsonClass.setHashtags(hashtags);
        System.out.println(JSON.toJSONString(generatedJsonClass));

        GeneratedJsonClass generatedJsonClass1 = JSON.parseObject(JSON.toJSONString(generatedJsonClass), GeneratedJsonClass.class);
        System.out.println("---------------------" + generatedJsonClass1);

        System.out.println("------------------------------------------");
        String str = "{'video_resource_id':'1765019257306548000','video_resource_id_cover':'http://ae-content.oss-cn-hangzhou.aliyuncs.com/1706514553101%E6%9D%AD%E5%B7%9E%E6%A2%85%E8%B5%9B%E5%BE%B7%E6%96%AF%E8%BD%A6%E4%B8%BB%E4%BA%A4%E6%B5%81%E7%BE%A43%202024-01-01%2013.59.19.mp4?x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast','cover_img_resource_id':'','cover_img_resource':'','locale':'国家','currency':'货币','content':'内容','hashtags':['旅行'],'items':[{'key':1706514546419,'itemId':'1111'}],'externalItems':[{'key':1706514546419,'source':'12','title':'223','itemUrl':'33','itemMainPic':'','itemPics':[],'itemPrice':''}],'email':'heyuting0714@gmail.com','emailName':'Toyboy','attributes':{'attributes':{'key':'value'}}}";
        GeneratedJsonClass generatedJsonClass2 = JSON.parseObject(str, GeneratedJsonClass.class);
        System.out.println(1111);
    }
}
